﻿Imports DTO, BUS
Public Class frmGhiNhanMatSach

    Dim dsSach As New List(Of SachDTO)
    Dim dsDocGia As New List(Of DocGiaDTO)
    Dim dsNhanVien As New List(Of NhanVienDTO)

    Private Sub frmGhiNhanMatSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sachBus As New SachBUS()
        Dim docgiaBus As New DocGiaBUS()
        Dim nhanvienBus As New NhanVienBUS()



        dsDocGia = docgiaBus.LayDSDocGia()
        dsSach = sachBus.LayDSSach()
        dsNhanVien = nhanvienBus.LayDSNhanVien_ThuThu()

        LookUpEdit_DocGia.Properties.DataSource = dsDocGia
        LookUpEdit_DocGia.Properties.ValueMember = "MaDocGia"
        LookUpEdit_DocGia.Properties.DisplayMember = "TenDocGia"
        LookUpEdit_DocGia.EditValue = 0

        LookUpEdit_NhanVienGhiNhan.Properties.DataSource = dsNhanVien
        LookUpEdit_NhanVienGhiNhan.Properties.ValueMember = "MaNhanVien"
        LookUpEdit_NhanVienGhiNhan.Properties.DisplayMember = "HoTen"
        LookUpEdit_NhanVienGhiNhan.EditValue = 0

        LookUpEdit_Sach.Properties.DataSource = dsSach
        LookUpEdit_Sach.Properties.ValueMember = "MaSach"
        LookUpEdit_Sach.Properties.DisplayMember = "TenSach"
        LookUpEdit_Sach.EditValue = 0

        DateTimePicker_NgayGhiNhan.Value = DateTime.Now()

    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim s As New SachDTO()
        For Each item In dsSach
            If item.MaSach = LookUpEdit_Sach.EditValue Then
                s = item
                Exit For
            End If
        Next
        _DataGridView_DanhSachSach.Rows.Add(s.MaSach, s.TenSach, s.MaTheLoai, s.MaTacGia, s.NamXuatBan, s.MaNhaSanXuat, s.NgayNhap, s.TriGia, s.MaTinhTrang)

    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        Dim i As Integer = _DataGridView_DanhSachSach.CurrentRow.Index
        _DataGridView_DanhSachSach.Rows.RemoveAt(i)
    End Sub

    Private Sub SimpleButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton5.Click
        Dim str As String = String.Format("{0:dd/MM/yyyy}", DateTimePicker_NgayGhiNhan.Value)

        Dim dong As Integer = _DataGridView_DanhSachSach.Rows.Count - 2
        For index As Integer = 0 To dong
            Dim sql As String = "insert into GHINHANMATSACH(MaSach,MaDocGia,TienPhat ,NgayGhiNhan,MaNhanVien) values ("
            sql = sql & _DataGridView_DanhSachSach.Rows(index).Cells(0).Value.ToString() & ", "
            sql = sql & LookUpEdit_DocGia.EditValue.ToString() & "," & _DataGridView_DanhSachSach.Rows(index).Cells(7).Value.ToString() & ", '"
            sql = sql & str & "'," & LookUpEdit_NhanVienGhiNhan.EditValue.ToString() & ")"

            MessageBox.Show(sql)
            Dim ghinhanBus As New GhiNhanMatSachBUS()
            Dim kq As Integer = ghinhanBus.GhiNhanMatSach(sql)

            If kq = 1 Then
                MessageBox.Show("Ghi nhận thành công !")
            Else
                MessageBox.Show("Ghi nhận thất bại !")

            End If

        Next

        
    End Sub

    Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub SimpleButton_TongTienPhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton_TongTienPhat.Click
        Dim n As Integer = _DataGridView_DanhSachSach.Rows.Count
        Dim tgtien As Integer = 0
        For index As Integer = 0 To n - 1
            tgtien += _DataGridView_DanhSachSach.Rows(index).Cells(7).Value
        Next
        TextEdit1.Text = tgtien.ToString()

    End Sub
End Class